#!/bin/bash

# DCarPai UBBC 运行脚本 (Linux/macOS)
echo "🚗 启动 DCarPai UBBC 规则管理系统..."
echo "=================================="

# 检查是否已安装依赖
if [ ! -d "node_modules" ]; then
    echo "📦 检测到未安装依赖，正在自动安装..."
    ./install.sh
    if [ $? -ne 0 ]; then
        echo "❌ 依赖安装失败"
        exit 1
    fi
fi

# 检查ubbc-rules.json是否存在
if [ ! -f "data/ubbc-rules.json" ]; then
    echo "⚠️  data/ubbc-rules.json 文件不存在，正在创建示例文件..."
    mkdir -p data
    cat > data/ubbc-rules.json << 'EOF'
{
    "rule1": {
        "rules-name": "竞品内容",
        "conditions": [
            {
                "content": "除了[懂车帝]以外的,汽车新闻类网站Logo",
                "value": true
            },
            {
                "content": "除了[懂车帝]三个字以外的,汽车新闻类网站标题",
                "value": true
            }
        ]
    },
    "rule2": {
        "rules-name": "反动内容",
        "conditions": [
            {
                "content": "打倒",
                "value": true
            },
            {
                "content": "蜗牛与黄鹂鸟",
                "value": true
            }
        ]
    }
}
EOF
    echo "✅ 示例规则文件已创建"
fi

echo "🚀 启动后端服务器..."
# 启动后端
node server/server.js &
BACKEND_PID=$!

# 等待后端启动
sleep 2
echo "✅ 后端服务器已启动 (http://localhost:3001)"

echo "🎨 启动前端开发服务器..."
# 启动前端
npm run dev &
FRONTEND_PID=$!

echo ""
echo "🎉 系统启动完成!"
echo "📱 前端地址: http://localhost:3000"
echo "🔧 后端API: http://localhost:3001"
echo ""
echo "💡 使用说明:"
echo "  - 在浏览器中打开 http://localhost:3000"
echo "  - 可以查看、编辑、添加和删除UBBC规则"
echo "  - 按 Ctrl+C 停止服务"
echo ""

# 等待用户中断
wait $FRONTEND_PID
